package com.shijianchen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijianchen.projo.ListActivityPage;
import com.shijianchen.projo.ListCategory;
import com.shijianchen.projo.SpCategory;
import com.shijianchen.projo.SpGoods2;
import com.shijianchen.projo.query.ListActivityQuery;
import com.shijianchen.projo.query.ListCategoryQuery;
import com.shijianchen.projo.query.SpGoodsQuery;
import com.shijianchen.service.ListActivityPageService;
import com.shijianchen.service.ListCategoryService;
import com.shijianchen.service.SpCategoryService;
import com.shijianchen.service.SpGoodsService;
import com.shijianchen.utils.QueryUtils;
import com.shijianchen.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("pro-api/auth/wechat")
public class WechatController {
    @Autowired
    private SpCategoryService spCategoryService;
    @Autowired
    private SpGoodsService spGoodsService;
    /**
     * 商品类别列表
     */
    @Autowired
    private ListCategoryService listCategoryService;


    @Autowired
    private ListActivityPageService listActivityPageService;

    /**
     * 支持分页
     * @param query
     * @return
     * @throws IllegalAccessException
     */
    @PostMapping("getCategory")
    public Result<Page<ListCategory>> getCategory(@RequestBody ListCategoryQuery query) throws IllegalAccessException {
        Page<ListCategory> page = new Page<>(query.getCurrent(),query.getSize());
        QueryUtils<ListCategoryQuery,ListCategory> queryUtils = new QueryUtils<>();
        String[] columns = {"insurance_category"};
        QueryWrapper<ListCategory> wrapper = queryUtils.getWrapper(query,columns);
        wrapper.orderBy(true,false,"id");
        Page<ListCategory> list = listCategoryService.page(page,wrapper);
        return Result.ok(list);
    }

    /**
     * 获取所有商品分类数据
     * @param query
     * @return
     * @throws IllegalAccessException
     */
    @PostMapping("getAllCategory")
    public Result<List<ListCategory>> getAllCategory(@RequestBody ListCategoryQuery query) throws IllegalAccessException {
        QueryUtils<ListCategoryQuery,ListCategory> queryUtils = new QueryUtils<>();
        QueryWrapper<ListCategory> wrapper = queryUtils.getWrapper(query);
        List<ListCategory> list = listCategoryService.list(wrapper);
        return Result.ok(list);
    }

    /**
     * 新增或修改
     * @param listCategory 入参实体类
     * @return Result<SpCategory>
     */
    @PostMapping("updateOrAddCateGory")
    public Result<Object> updateOrAddCateGory(@RequestBody ListCategory listCategory){
        boolean bool;
        if(listCategory.getId()>0){
            bool = listCategoryService.updateById(listCategory);
        }else{
            bool = listCategoryService.save(listCategory);
        }
        if(bool){
            return Result.ok("请求成功");
        }else{
            return Result.fail("请求失败");
        }
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("deleteCategory/{id}")
    public Result<SpCategory> deleteCategory(@PathVariable Long id){
        boolean b = spCategoryService.removeById(id);
        if(b){
            return Result.ok("删除成功");
        }else{
            return Result.fail("删除失败");
        }
    }

    @PostMapping("gooods/list")
    public Result<Object> getGoodsList(@RequestBody SpGoodsQuery query) throws IllegalAccessException {
        Page<SpGoods2> page = new Page<>(query.getCurrent(),query.getSize());
        QueryUtils<Object, SpGoods2> queryUtils = new QueryUtils<>();
        QueryWrapper<SpGoods2> wrapper = queryUtils.getWrapper(query);
        Page<SpGoods2> list = spGoodsService.page(page, wrapper);
        return Result.ok(list);
    }
    /**
     * 获取十大分类数据
     *
     * @param query
     * @return
     * @throws IllegalAccessException
     */
    @PostMapping("getAllActivity")
    public Result<List<ListActivityPage>> getAllActivity(@RequestBody ListActivityQuery query) throws IllegalAccessException {
        QueryUtils<ListActivityQuery,ListActivityPage> queryUtils = new QueryUtils<>();
        QueryWrapper<ListActivityPage> wrapper = queryUtils.getWrapper(query);
        List<ListActivityPage> list = listActivityPageService.list(wrapper);
        return Result.ok(list);
    }
}
